package com.infitecs.eyas.judge;

import com.infitecs.eyas.judge.model.ReportRecord;
import com.infitecs.eyas.judge.service.JudgeService;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.quality.service.AshService;
import com.infitecs.eyas.quality.service.BurstService;
import com.infitecs.eyas.quality.service.ElementService;
import com.infitecs.eyas.quality.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Demon on 2015/5/8.
 */
@Controller
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private IndustryService industryService;
    @Autowired
    private ElementService elementService;
    @Autowired
    private BurstService burstService;
    @Autowired
    private AshService ashService;
    @Autowired
    private JudgeService judgeService;

    @RequestMapping("/getQualityReport")
    public String getReport(HttpSession session,Model model){
        Object tempCaseId = session.getAttribute("caseId");
        if(tempCaseId == null){
            return "report/report";
        }
        int caseId = Integer.parseInt(tempCaseId.toString());
        Industry industry = industryService.getIndustry(caseId);
        Element element = elementService.getElement(caseId);
        Burst burst = burstService.getBurst(caseId);
        Ash ash = ashService.getAsh(caseId);
        List<ReportRecord> records = judgeService.getReportByCaseId(caseId);
        if(records.isEmpty()) {
            judgeService.judgeE10ByJudgeCase1(industry.getVdaf(), caseId);
            judgeService.judgeE10ByJudgeCase2(industry.getVdaf(), caseId);
            judgeService.judgeE15ByJudgeCase2(element.getQnetAr(), caseId);
            judgeService.judgeE3ByJudgeCase2(industry.getMar(), caseId);
            judgeService.judgeE54ByJudgeCase2(ash.getSt(), caseId);
            judgeService.judgeE57ByJudgeCase3(ash.getRt(), caseId);
            judgeService.judgeE80ByJudgeCase4(burst.getKd(), caseId);
            judgeService.judgeE21ByJudgeCase5(burst.getHgi(), caseId);
            judgeService.judgeE23ByJudgeCase6(burst.getKe(), caseId);
            judgeService.judgeE24ByJudgeCase6(burst.getAi(), caseId);
            judgeService.judgeE85ByJudgeCase7(ash.getR2(), caseId);
            judgeService.judgeE50ByJudgeCase8(burst.getKgc(), caseId);
            judgeService.judgeE51ByJudgeCase9(burst.getHm(), caseId);
        }
        records = judgeService.getReportByCaseId(caseId);
        model.addAttribute("reportList",records);
        return "report/report";
    }
}
